Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relax some shape checks for rank-1 tensors #625

Merged
merged 2 commits into from
Mar 2, 2024

Conversation

AngelEzquerra
Copy link
Contributor

Consider that rank-1 tensors are "shapeless" and can fit both into [1, N] and [N, 1] tensors, for example.

This greatly reduces the need to use .reshape after converting a seq or array into a tensor in assignments, and reduces errors that happen at runtime for code that is conceptually correct. For example, with this change this will work:

var t = eye(3, 3)
t[_,0] = [1, 2, 3].toTensor

Consider that rank-1 tensors are "shapeless" and can fit both into [1, N] and [N, 1] tensors, for example.

This greatly reduces the need to use .reshape after converting a seq or array into a tensor in assignments, and reduces errors that happen at runtime for code that is conceptually correct. For example, with this change this will work:
```
var t = eye(3, 3)
t[_,0] = [1, 2, 3].toTensor
```
@@ -175,6 +200,19 @@ proc main() =

check: y == exp

block:
# Assigning a rank-1 tensor into an axis of the same size is supported
# Note that this is not supported by numpy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does numpy have a good reason not to support this maybe?

@Vindaar
Copy link
Collaborator

Vindaar commented Mar 2, 2024

Having digested it now, I like it. As it is a breaking change, I made the hardcoded true value based on a CT variable, RelaxedRankOne, which can be disabled by compiling with -d:RelaxedRankOne=false to recover the old behavior.

@Vindaar Vindaar merged commit 6a41201 into mratsim:master Mar 2, 2024
6 checks passed
@AngelEzquerra AngelEzquerra deleted the rank1_tensor_assignment branch March 2, 2024 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants